using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInAirState : PlayerState
{
    public PlayerInAirState(Player player, PlayerMoveData playerData, StateMachine stateMachine, string animBoolName) : base(player, playerData, stateMachine, animBoolName)
    {
        stateType = StateType.InAir;
    }

    private bool isJumping;
    private bool CanJumpDelay;

    public override void LogicUpdate()
    {
        base.LogicUpdate();

        ControlJumpDelay();
        ControlJumpHeightByHoldTime();

        if (dashInput && !isTouchingWall && player.DashState.CanDash())
        {
            if (xInput != 0 || yInput != 0)
            {
                player.DashState.SetDashDirection(new Vector2Int(xInput, yInput));
            }
            else
            {
                player.DashState.SetDashDirection(new Vector2Int(player.FacingDirection, 0));
            }
            stateMachine.ChangeState(player.DashState);
        }
        else if (isTouchingWall && jumpInput && xInput == player.FacingDirection)
        {
            stateMachine.ChangeState(player.WallJumpState);
        }
        else if (isTouchingWall && jumpInput)
        {
            stateMachine.ChangeState(player.WallNegativeJumpState);
        }
        else if (isTouchingWall && !isTouchingLedge && !isGrounded)
        {
            stateMachine.ChangeState(player.LedgeClimbState);
        }
        else if (isTouchingWall && isTouchingLedge && grabInput)
        {
            stateMachine.ChangeState(player.WallGrabState);
        }
        else if (isTouchingWall && xInput == player.FacingDirection && player.CurrentVelocity.y < playerData.wallSlideThehold)
        {
            stateMachine.ChangeState(player.WallSlideState);
        }
        else if (jumpInput && player.JumpState.CanJump())
        {
            stateMachine.ChangeState(player.JumpState);
        }
        else if (isGrounded && player.CurrentVelocity.y < 0.01f)
        {
            stateMachine.ChangeState(player.IdleState);
        }
        else
        {
            player.CheckIfNeedFlip(xInput);
            player.SetVelocityX(xInput * playerData.movementVelocity * playerData.movementInAirMultiplier);

            player.animator.SetFloat("YVelocity", player.CurrentVelocity.y);
        }
    }

    public override void OnCheck()
    {
        base.OnCheck();

        isLeftFootGrounded = player.CheckLeftFootIfGrounded();
        isRightFootGrounded = player.CheckRightFootIfGrounded();
        isGrounded = isLeftFootGrounded || isRightFootGrounded;
        isSingleFootGrounded = isLeftFootGrounded && !isRightFootGrounded || !isLeftFootGrounded && isRightFootGrounded;
        isTouchingWall = player.CheckIsTouchingWall();
        isTouchingLedge = player.CheckIsTouchingLedge();

        if (isTouchingWall && !isTouchingLedge)
        {
            player.LedgeClimbState.SetdetecedPos(player.transform.position);
        }
    }


    public void SetIsJumping() => isJumping = true;

    private void ControlJumpHeightByHoldTime() 
    {
        if (isJumping)
        {
            if (jumpInputStop)
            {
                player.SetVelocityY(player.CurrentVelocity.y * playerData.jumpHeightMultiplier);
                isJumping = false;
            }
            else if (Mathf.Abs(player.CurrentVelocity.y) < 0.1f)
            {
                isJumping = false;
            }
        }

    }


    public void SetCanJumpDelay() => CanJumpDelay = true;

    private void ControlJumpDelay() 
    {
        if (CanJumpDelay)
        {
            if (Time.time - stateEnterTime >= playerData.maxJumpDelay)
            {
                player.JumpState.SetJumpCountZero();
            }
        }
    }
}
